package com.example.java.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.java.entity.News;
import com.example.java.mapper.NewsMapper;
import com.example.java.service.INewsService;
import com.example.java.utils.QiniuImageUtils;
import com.example.java.utils.Result;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private QiniuImageUtils qiniuImageUtils;

    @Autowired
    private INewsService iNewsService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result news_list(@RequestBody News news, @RequestParam int currentPage, @RequestParam int pageSize) {
//        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByDesc("id");
//        List<News> newsList = newsMapper.selectList(queryWrapper);
//        for (News news : newsList) {
//            String imageUrl = news.getPic();
//            String pic = "";
//            if (StringUtils.isNotBlank(imageUrl)) {
//                pic = qiniuImageUtils.generatePrivateImageUrl(imageUrl);
//            }
//            news.setPic(pic);
//        }
        IPage<News> newsList = iNewsService.getDataByPage(currentPage, pageSize, news);
        for (News item : newsList.getRecords()) {
            String pic = item.getPic();
            String real_pic = "";
            if (StringUtils.isNotBlank(pic)) {
                real_pic = qiniuImageUtils.generatePrivateImageUrl(pic);
                item.setPic(real_pic);
            }
        }
        return Result.ok().data("data", newsList);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public Result getNewsDetail(@RequestBody News news) {
        News news1 = newsMapper.selectById(news.getId());
        news1.setPic(qiniuImageUtils.generatePrivateImageUrl(news1.getPic()));
        return Result.ok().data("data", news1);
    }

}
